package android.tusnotas.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.tusnotas.beans.Property;

public class PropertyModel {

//INSERTS
	public long insertProperty(String key, String value) {
		TusNotasModel.propertyInsertStmt.bindString(1, key);
		TusNotasModel.propertyInsertStmt.bindString(2, value);
		return TusNotasModel.propertyInsertStmt.executeInsert();
	}
	
	public long insertProperty(Property property) {
		TusNotasModel.propertyInsertStmt.bindString(1, property.getKey());
		TusNotasModel.propertyInsertStmt.bindString(1, property.getValue());
		try{
			return TusNotasModel.propertyInsertStmt.executeInsert();
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
//END INSERTS
	
	
//UPDATES	
	public int updateProperty(String key, String value) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("value", value);
		return TusNotasModel.db.update(TusNotasModel.PROPERTY_TABLE_NAME, contentValues, "key = ?" , new String[]{key});
	}
	
	public int updateProperty(Property property) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("value", property.getValue());
		return TusNotasModel.db.update(TusNotasModel.PROPERTY_TABLE_NAME, contentValues, "key = ?" , new String[]{property.getKey()});
	}
//END UPDATES
	
	
//DELETES
	public void deleteAllProperties() {
		TusNotasModel.db.delete(TusNotasModel.PROPERTY_TABLE_NAME, null, null);
	}
	
	public void deletePropertyById(Integer id) {
		TusNotasModel.db.delete(TusNotasModel.PROPERTY_TABLE_NAME, "id = "+id, null);
	}
	
	public void deletePropertyByKey(String id) {
		TusNotasModel.db.delete(TusNotasModel.PROPERTY_TABLE_NAME, "key = '"+id+"'", null);
	}
//END DELETES

	
//SELECTS
	public List<Property> selectAllProperties() {
		List<Property> properties = new ArrayList<Property>();
		Property property = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.PROPERTY_TABLE_NAME, new String[] {"id", "key", "value"},
				null, null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				property = new Property();
				property.setId(cursor.getInt(0));
				property.setKey(cursor.getString(1));
				property.setValue(cursor.getString(2));
				properties.add(property);
				
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return properties;
	}
	
	public Property selectProperyByKey(String key) {
		Property property = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.PROPERTY_TABLE_NAME, new String[] {"id", "key", "value"},
				"key ='" + key +"'", null, null, null, "id ASC", "1");
		
		if (cursor.moveToFirst()) {
			do {
				property = new Property();
				property.setId(cursor.getInt(0));
				property.setKey(cursor.getString(1));
				property.setValue(cursor.getString(2));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return property;
	}
}